package com.cooper.service.project;

import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.dao.project.ProjectDAO;
import com.cooper.entity.ProjectEntity;
import com.cooper.pagination.PageInfo;
import com.cooper.pagination.PageResult;
import com.cooper.vo.CompositeDesignerProjectVo;
import com.cooper.vo.ProjectVo;

@Service
public class ProjectService {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ProjectDAO projectDAO;
	
	public void addProject(ProjectEntity projectDo){
		try {
			projectDAO.addProjectDo(projectDo);
		} catch (Exception e) {
			logger.error("ProjectService-addProject-error", e);
		}
	}
	
	public void updateProject(ProjectEntity projectDo){
		try {
			projectDAO.updateProject(projectDo);
		} catch (Exception e) {
			logger.error("ProjectService-updateProject-error", e);
		}
	}
	
	public ProjectEntity findProjectById(Long id){
		try {
			return projectDAO.getProjectDo(id);
		} catch (Exception e) {
			logger.error("ProjectService-addDisplayPicConfig-error", e);
		}
		return null;
	}
	
	public ProjectEntity findProjectByUserId(Long userId){
		try {
			return projectDAO.getProjectDoByUserId(userId);
		} catch (Exception e) {
			logger.error("ProjectService-addDisplayPicConfig-error", e);
		}
		return null;
	}
		
	@SuppressWarnings("unchecked")
	public PageResult<ProjectVo> findProjects(ProjectVo queryParamVo, PageInfo pageInfo){
		int rowCount = this.projectDAO.queryProjectsCount(queryParamVo);
		pageInfo.setRowCount(rowCount);
		if(0 == rowCount){
			return new PageResult<ProjectVo>(Collections.EMPTY_LIST, pageInfo);
		}
		List<ProjectVo> dataList = projectDAO.queryProjects(queryParamVo, pageInfo);
		return new PageResult<ProjectVo>(dataList, pageInfo);
	}
	
	
	@SuppressWarnings("unchecked")
	public PageResult<CompositeDesignerProjectVo> findCompositeDesignerProjects(CompositeDesignerProjectVo queryParamVo, PageInfo pageInfo){
		int rowCount = this.projectDAO.queryProjectsCount(queryParamVo);
		pageInfo.setRowCount(rowCount);
		if(0 == rowCount){
			return new PageResult<CompositeDesignerProjectVo>(Collections.EMPTY_LIST, pageInfo);
		}
		List<CompositeDesignerProjectVo> dataList = projectDAO.queryProjects(queryParamVo, pageInfo);
		return new PageResult<CompositeDesignerProjectVo>(dataList, pageInfo);
	}
}
